package com.ruoyi.web.controller.mq.order;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

@Component
public class DirectMsgSender {


    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Value("${ruoyi.rabbitmq.api.order.queue}")
    private String apiOrderQueue;


    @Value("${ruoyi.rabbitmq.api.max-size}")
    private Integer maxSize;

    /**
     * 发送用户注册认证校验消息
     * @param message 消息体
     * @param size 队列大小
     */
    public void sendOrderAuthQueue(String message) {
        int index = calcQueueIndex();
        String format = MessageFormat.format(apiOrderQueue, index);
        rabbitTemplate.convertAndSend(format, message);
    }

    /**
     * 计算存储队列index
     * @param
     * @return
     */
    private int calcQueueIndex() {
        return (int)(Math.random() * maxSize);
    }

}
